package com.ruitu.mall_98_2.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.beanu.arad.base.BaseModel;
import com.beanu.arad.base.BasePresenter;
import com.beanu.arad.base.ToolBarFragment;
import com.beanu.arad.utils.NetworkUtils;
import com.ruitu.mall_98_2.R;

import java.util.List;

public class BaseFragment<T extends BasePresenter, E extends BaseModel> extends ToolBarFragment<T, E> {
    private LinearLayout ll_net_status;
    private LinearLayout ll_order_status;
    private LinearLayout ll_error;
    private TextView tv_agin;
    private List data;
    private RecyclerView recyclerView;

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ll_net_status = view.findViewById(R.id.ll_net_status);
        ll_order_status = view.findViewById(R.id.ll_order_status);
        ll_error = view.findViewById(R.id.ll_error);
        tv_agin = view.findViewById(R.id.tv_agin);
        ll_net_status.setOnClickListener(this);
        tv_agin.setOnClickListener(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (NetworkUtils.isConnected()) {
            ll_net_status.setVisibility(View.GONE);
        } else {
            ll_net_status.setVisibility(View.VISIBLE);
        }
    }

    protected void setData(boolean isDataEmpty, RecyclerView recyclerView) {
        this.recyclerView = recyclerView;
        if (isDataEmpty) {
            ll_order_status.setVisibility(View.VISIBLE);
        } else {
            ll_order_status.setVisibility(View.GONE);
        }
        ll_error.setVisibility(View.GONE);
        if (recyclerView != null) {
            if (recyclerView.getVisibility() == View.GONE) {
                recyclerView.setVisibility(View.VISIBLE);
            }
        }
    }

    protected void showError() {
        if (ll_net_status.getVisibility() == View.VISIBLE) {
            ll_net_status.setVisibility(View.GONE);
        }
        ll_error.setVisibility(View.VISIBLE);
        showRecycleView();
    }

    private void showRecycleView() {
        if (recyclerView != null) {
            if (recyclerView.getVisibility() == View.VISIBLE) {
                recyclerView.setVisibility(View.GONE);
            }
        }
    }


    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v == ll_net_status) {
            getReData();
        }

        if (tv_agin == v) {
            getReData();
        }
    }

    protected void getReData() {
        if (NetworkUtils.isAvailableByPing()) {
            ll_net_status.setVisibility(View.GONE);
        } else {
            ll_net_status.setVisibility(View.VISIBLE);
            if (ll_net_status.getVisibility() == View.VISIBLE) {
                ll_net_status.setVisibility(View.GONE);
            }
            showRecycleView();
        }
    }
}
